package org.xebia.roo.addon.performancemonitor;

import org.springframework.roo.process.manager.FileManager;
import org.springframework.roo.process.manager.MutableFile;
import org.springframework.roo.support.util.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Taken from Spring WebFlow Roo addon
 * 
 */
public class XmlTemplate {
	private FileManager fileManager;

	public XmlTemplate(FileManager fileManager) {
		this.fileManager = fileManager;
	}

	/**
	 * Wraps common code for updating an XML file. Callers provide a callback and focus on 
	 * the actual changes without having to know the rest of the details.
	 * 
	 * @param resolvedPathIdentifier the path to the XML file to update.
	 * @param rootElementCallback A callback with the logic that needs to be applied.
	 */
	public void update(String resolvedPathIdentifier, DomElementCallback rootElementCallback) {
		MutableFile mutableFile = fileManager.updateFile(resolvedPathIdentifier);
		Document document = null;
		try {
			document = XmlUtils.getDocumentBuilder().parse(mutableFile.getInputStream());
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
		Element root = document.getDocumentElement();
		if (rootElementCallback.doWithElement(document, root)) {
			XmlUtils.writeXml(mutableFile.getOutputStream(), document);
		}
	}

	public interface DomElementCallback {

		/**
		 * Use this method to provide logic for updating a {@link Document}.
		 * 
		 * @param document the document
		 * @param rootElement the root element of the document.
		 * @return true if any changes were made that require saving, false otherwise
		 */
		boolean doWithElement(Document document, Element rootElement);
	}
}
